package com.zhkj.txg.module.login.ui

import android.util.Log
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.zhkj.lib.base.BaseActivity
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.txg.R
import com.zhkj.txg.base.CustomApplication
import com.zhkj.txg.module.login.entity.BannerEntity
import com.zhkj.txg.module.login.vm.GuideViewModel
import com.zhkj.txg.utils.ImageUtils
import kotlinx.android.synthetic.main.activity_guide.*
import kotlinx.android.synthetic.main.activity_splash.*
import java.util.*
import kotlin.math.max
import kotlin.math.min

class GuideActivity : ViewModelActivity<GuideViewModel>() {

    override fun setViewModel(): GuideViewModel = createViewModel()

    override val layoutResId: Int = R.layout.activity_guide

    override fun initUI() {


        tvGuidePrevious.setOnClickListener {
            vpGuide.currentItem = max(vpGuide.currentItem - 1, 0)
        }
        tvGuideNext.setOnClickListener {
            if (vpGuide.currentItem < 2) vpGuide.currentItem = min(vpGuide.currentItem + 1, 3)
            else {
                LoginActivity.open2Login(this)
                finish()
            }
        }

        vpGuide.registerOnPageChangeCallback(object : OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                if (position == 3) {
                    tvGuideImmediately.visibility = View.VISIBLE
                } else {
                    tvGuideImmediately.visibility = View.GONE
                }
            }
        })

        tvGuideImmediately.setOnClickListener {
            LoginActivity.open2Login(this)
            finish()
        }
    }

    override fun initData() {
        viewModel.getBanner(CustomApplication.getShopId().toString(), "3")
    }

    override fun initViewModel() {

        viewModel.bannerLiveData.observe(this, Observer { response ->
            handlerResponseStatus(response.status, response.statusTip)
            if (response.status == HttpResponseStatus.Status.SUCCESS) {
                val data = response.response?.data

                if (data == null) {
                    LoginActivity.open2Login(this)
                    finish()
                } else {
                    data.reverse()
                    with(vpGuide) {
                        adapter = object : FragmentStateAdapter(this@GuideActivity) {
                            override fun getItemCount(): Int = 4
                            override fun createFragment(position: Int): Fragment {
                                return GuideFragment.newInstance(data[position].image)
                            }
                        }
                        getChildAt(0)?.overScrollMode = View.OVER_SCROLL_NEVER
                    }
                }
            } else if (response.status == HttpResponseStatus.Status.FAILURE) {
                LoginActivity.open2Login(this)
                finish()
            }
        })
    }
}